/**
 * 经纬度只保留小数点后6位
 * @param {*} n
 * @returns
 */
export const cutPostion = n => {
  n = n.toString()

  let dotPosition = n.indexOf('.')

  if (dotPosition !== -1) {
    return Number(n.substring(0, dotPosition + 7))
  }

  return n
}

/**
 * 获取地图边界，当用户拖动地图，边际会实时更新
 * @param {MapInstance} mapInstance 地图实例对象
 * @returns {Object} 返回地图左上角和右下角边界经纬度
 * @link https://lbs.qq.com/webApi/javascriptGL/glDoc/docIndexMap
 */
export const getMapBounds = (mapInstance) => {
  const bounds = mapInstance.getBounds()

  if (bounds) {
    return {
      ne: bounds.getNorthEast(),
      sw: bounds.getSouthWest()
    }
  } else {
    return {}
  }
}

/**
 * 是否有加载过腾讯地图sdk
 */
export const hasSDK = (id = 'tengxun-map') => {
    let hasMapSDK = document.querySelector(`#${id}`)
    return hasMapSDK ? true : false
}
